import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_plugin_demo/flutter_plugin_demo.dart';
import 'package:flutterappmydemo/base/base_stateless_widget.dart';

class PluginPage extends StatefulWidget {
  @override
  _PluginPageState createState() => _PluginPageState();
}

class _PluginPageState extends State<PluginPage> {
  // static const MethodChannel _channel = const MethodChannel('flutter_plugin_demo_kotlin');
  static const MethodChannel _channel =
      const MethodChannel('flutter_plugin_demo_java');

  static Future<String> showToast(String text) async {
    final String str = await _channel
        .invokeMethod('showToast', <String, dynamic>{'text': text});
    return str;
  }

  static Future<String> openActivity() async {
    final String str = await _channel.invokeMethod('openActivity');
    return str;
  }

  static const myEventPlugin = const EventChannel('flutter_plugin_demo_java1');

  @override
  void initState() {
    myEventPlugin.receiveBroadcastStream().listen(
      (event) {
        Map<dynamic, dynamic> map = event;
        var userid = map["userid"];
        var age = map["age"];
        print(
            'PluginPage.initState Android层调用flutter层方法 age=$age userid=$userid');
      },
      onError: (Object error) {
        print(
            'PluginPage.initState Android层调用flutter层方法 error=${error.toString()}');
      },
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("插件的使用"),
        centerTitle: true,
        leading: IconButton(
          icon: Icon(Icons.arrow_back, color: Colors.white),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ),
      body: Container(
        decoration: BoxDecoration(
          color: Colors.blue,
        ),
        child: Column(
          children: <Widget>[
            Text(
                "切记：编辑开发插件相关的功能时，打开项目，要打开插件项目中 example中的 Android项目 \n不然项目报红，编辑代码无提示"),

            /* RaisedButton(
            child: Text("点击"),
            onPressed: () async {
              streamController.sink.add(++_counter);

              var platformVersion = await FlutterPluginDemo.platformVersion;
              print("PluginPage.getBody platformVersion=$platformVersion");
            },
          ),
          StreamBuilder<int>(
            stream: streamController.stream,
            initialData: _counter,
            builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
              return Text('12222=: ${snapshot.data} times');
            },
          ),*/

            RaisedButton(
              child: Text("调用插件方法toast"),
              onPressed: () async {
                FlutterPluginDemo.showToast("调用插件方法");
              },
            ),

            ///java版和kotlin版二者只能选其一
            RaisedButton(
              child: Text("调用插件方法toast java版"),
              onPressed: () async {
                showToast("调用插件方法java版");
              },
            ),
            /* RaisedButton(
            child: Text("调用插件方法toast kotlin版"),
            onPressed: () async {
              showToast("调用插件方法kotlin版");
            },
          ),*/

            RaisedButton(
              child: Text("调用插件方法openActivity"),
              onPressed: () async {
                openActivity();
              },
            ),
          ],
        ),
      ),
    );
  }
}
